package com.tduck.cloud.datav.service.charts;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import com.google.common.collect.Lists;
import com.tduck.cloud.datav.request.ChartDataRequest;
import com.tduck.cloud.datav.service.BaseChartDataService;
import com.tduck.cloud.report.data.enums.AggsTypeEnum;
import com.tduck.cloud.report.data.params.ValueQueryParams;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 指标图
 *
 * @author tduck
 */
@Component
@Slf4j
public class PieOneValueChartDataService extends BaseChartDataService {


    @Override
    public Map<String, Object> getChartData(ChartDataRequest request) {
        ChartDataRequest.IndexField field = request.getIndex().get(0);
        Map<String, Object> result = MapUtil.of();
        result.put("label", field.getLabel());
        ValueQueryParams queryParams=new ValueQueryParams(field.getId(),field.getAggregateType(),field.getValue());
        queryParams.setQueryObjName(request.getIndexName());
        queryParams.setFilter(Lists.newArrayList(request.getFilter()));
        result.put("total", dataSearchTemplate.getFieldTotalValue(queryParams));
        if (field.getAggregateType() == AggsTypeEnum.SPECIFIED && ObjectUtil.isNotNull(field.getValue())) {
            result.put("value", dataSearchTemplate.getFieldValueCount(queryParams));
        } else if (null != field.getAggregateType()) {
            result.put("value", dataSearchTemplate.getFieldAggregateValue(queryParams));
        }
        return result;
    }


}
